﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Routing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gear.Web.Api.DynamicDataApi
{
    public class BrandRouteValueTransformer : DynamicRouteValueTransformer
    {
        public override async ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values)
        {
            if (!values.ContainsKey("controller") || !values.ContainsKey("action")) return await new ValueTask<RouteValueDictionary>(values);
            
            // 装配控制器controller
            ConfigureApplicationService(httpContext,values);
            return await new ValueTask<RouteValueDictionary>(values);
        }

        private void ConfigureApplicationService(HttpContext httpContext,RouteValueDictionary values)
        {
            ConfigureApiExplorer(values); //API检查
            ConfigureSelector(httpContext, values); //API注册
        }

        private void ConfigureApiExplorer(RouteValueDictionary values)
        {

            var Values=values.Values.ToArray();
            var controller = Values[0].ToString();
            var action = Values[1].ToString();

            return;


        }
        private void ConfigureSelector(HttpContext httpContext, RouteValueDictionary values)
        {
            //httpContext.Items.Add("Data_Driven_VisionTag", values["vision"]);
            httpContext.Items.Add("Data_Driven_ActionName", values["action"]);
            httpContext.Items.Add("Data_Driven_ControllerName", values["controller"]);
            values["controller"] = "DynamicDriven";
            values["action"] = "DriveMethod";
        }
    }
}

